                    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdvanceSearchView.java
 *
 * Created on 3-Apr-2010, 6:11:17 PM
 */
package ticketmaster.presentation.customer.purchase;

import ticketmaster.presentation.customer.purchase.SearchView;
import com.michaelbaranov.microba.calendar.DatePicker;
import java.awt.Insets;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import ticketmaster.businesslogic.SearchController;
import ticketmaster.dao.*;
import ticketmaster.dataobject.*;
import ticketmaster.presentation.customer.CustomerBasePanel;
import ticketmaster.util.DateTimeHelper;

/**
 *
 * @author Xiaolong Xue
 */
public class AdvanceSearchView extends CustomerBasePanel
{

    /** Creates new form AdvanceSearchView */
    public AdvanceSearchView()
    {
        initComponents();
        initDatePicker();
        addDataToComboBox();
    }

    private void addDataToComboBox()
    {
        Collection<Venue> venues = DAOFactory.getInstance().getVenueDAO().getVenuesDO(new Venue());

        Iterator<Venue> venueIterator = venues.iterator();

        while (venueIterator.hasNext())
        {
            Venue venue = venueIterator.next();
            cmbVenue.addItem(venue.getName());
            boolean locationAdded = false;
            for (int i = 0; i < cmbLocation.getItemCount(); ++i)
            {
                if (venue.getLocation().compareTo((String) cmbLocation.getItemAt(i)) == 0)
                {
                    locationAdded = true;
                    break;
                }
            }
            if (!locationAdded)
            {
                cmbLocation.addItem(venue.getLocation());
            }
        }

        String[] events =
        {
            "", "Ice Hockey - Women", "Ice Hockey - Men", "Figure Skating","Curling"
        };
        for (int i = 0; i < events.length; ++i)
        {
            cmbEvent.addItem(events[i]);
        }

//        String[] day = {"01","02","03","04","05","06","07","08",
//                            "09","10","11","12","13","14","15","16",
//                            "17","18","19","20","21","22","23","24",
//                            "25","26","27","28","29","30","31"};
//        String[] month = {"02","03"};
//        String[] year = {"2010"};

//        for(int i = 0; i < day.length; ++i)
//        {
//            cmbFromDay.addItem(day[i]);
//            cmbToDay.addItem(day[i]);
//        }
//        for(int i = 0; i < month.length; ++i)
//        {
//            cmbFromMonth.addItem(month[i]);
//            cmbToMonth.addItem(month[i]);
//        }
//        for(int i = 0; i < year.length; ++i)
//        {
//            cmbFromYear.addItem(year[i]);
//            cmbToYear.addItem(year[i]);
//        }

        cmbVenue.setSelectedIndex(-1);
        cmbLocation.setSelectedIndex(-1);
        cmbEvent.setSelectedIndex(0);
//        cmbFromDay.setSelectedIndex(0);
//        cmbFromMonth.setSelectedIndex(0);
//        cmbFromYear.setSelectedIndex(0);
//        cmbToDay.setSelectedIndex(30);
//        cmbToMonth.setSelectedIndex(1);
//        cmbToYear.setSelectedIndex(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblVenue = new javax.swing.JLabel();
        cmbVenue = new javax.swing.JComboBox();
        lblLocation = new javax.swing.JLabel();
        cmbLocation = new javax.swing.JComboBox();
        lblEvent = new javax.swing.JLabel();
        cmbEvent = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();
        lblFrom = new javax.swing.JLabel();
        lblTo = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        tfKeyword = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(AdvanceSearchView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblVenue.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        lblVenue.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblVenue.setText(resourceMap.getString("lblVenue.text")); // NOI18N
        lblVenue.setName("lblVenue"); // NOI18N
        add(lblVenue, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 60, 30));

        cmbVenue.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        cmbVenue.setName("cmbVenue"); // NOI18N
        cmbVenue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbVenueActionPerformed(evt);
            }
        });
        add(cmbVenue, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 110, 370, 60));

        lblLocation.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        lblLocation.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblLocation.setText(resourceMap.getString("lblLocation.text")); // NOI18N
        lblLocation.setName("lblLocation"); // NOI18N
        add(lblLocation, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 200, 70, 34));

        cmbLocation.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        cmbLocation.setName("cmbLocation"); // NOI18N
        add(cmbLocation, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 190, 370, 60));

        lblEvent.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        lblEvent.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblEvent.setText(resourceMap.getString("lblEvent.text")); // NOI18N
        lblEvent.setName("lblEvent"); // NOI18N
        add(lblEvent, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 290, 50, -1));

        cmbEvent.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        cmbEvent.setName("cmbEvent"); // NOI18N
        add(cmbEvent, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 270, 370, 60));

        btnSearch.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        btnSearch.setText(resourceMap.getString("btnSearch.text")); // NOI18N
        btnSearch.setName("btnSearch"); // NOI18N
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        add(btnSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 110, 160, 60));

        lblFrom.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        lblFrom.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblFrom.setText(resourceMap.getString("lblFrom.text")); // NOI18N
        lblFrom.setName("lblFrom"); // NOI18N
        add(lblFrom, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 450, -1, -1));

        lblTo.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        lblTo.setText(resourceMap.getString("lblTo.text")); // NOI18N
        lblTo.setName("lblTo"); // NOI18N
        add(lblTo, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 450, 70, -1));

        jLabel1.setFont(resourceMap.getFont("btnSearch.font")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 360, 80, 40));

        tfKeyword.setFont(resourceMap.getFont("tfKeyword.font")); // NOI18N
        tfKeyword.setText(resourceMap.getString("tfKeyword.text")); // NOI18N
        tfKeyword.setName("tfKeyword"); // NOI18N
        add(tfKeyword, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 350, 370, 60));

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 25, -1, -1));

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 190, 160, 60));
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        //Collection<Event> events = null;
        advanceSearchEvent();
    }//GEN-LAST:event_btnSearchActionPerformed

    private void cmbVenueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbVenueActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbVenueActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        SearchView sv = new SearchView();

        this.getPnlParent().removeAll();
        this.getPnlParent().add(sv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
        sv.setPnlParent(getPnlParent());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void advanceSearchEvent()
    {
        Timestamp startDateTS = DateTimeHelper.DateToTimestamp(startDate.getDate());
        Timestamp endDateTS = DateTimeHelper.DateToTimestamp(endDate.getDate());

        if (startDateTS.after(endDateTS))
        {
            JOptionPane.showMessageDialog(null, "start date cannot be after "
                                                + "end date", "Search Event", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        String eventName = (String) cmbEvent.getSelectedItem();
        String venueName = (String) cmbVenue.getSelectedItem();
        String location = (String) cmbLocation.getSelectedItem();
        String keyword = this.tfKeyword.getText().trim();


        EventView event = new EventView();
        event.setName(eventName);
        event.setBeginDateTime(startDateTS);
        event.setEndDateTime(endDateTS);
        event.setVenueName(venueName);
        event.setKeywords(keyword);
        event.setLocation(location);

        Collection<EventView> events = null;
        SearchController controller = new SearchController();
        events = controller.advancedSearch(event);
        SearchResultsView srv = new SearchResultsView(events);
        this.getPnlParent().removeAll();
        this.getPnlParent().add(srv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
        srv.setPnlParent(this.getPnlParent());

        // here should be a DAO to get all events
        // create a sql in EventViewDAO
        // events = DAOFactory.getInstance().getEventViewDAO().getEventsDOByCombined();

        /*
        Collection<Venue> venues = DAOFactory.getInstance().getVenueDAO().getVenuesDO(new Venue());
        if(venues.size() == 0)
        {
        JOptionPane.showMessageDialog(null, "No specified venue found ",
        "Advanced Search Event", JOptionPane.INFORMATION_MESSAGE);
        return;
        }


        
        String eventKeyword = (String) cmbEvent.getSelectedItem();

        Event event = new Event();
        event.setKeywords(eventKeyword);
        event.setBeginDateTime(startDateTS);
        event.setEndDateTime(endDateTS);

        //Collection<Event> events = null;

        Iterator<Venue> venueIterator = venues.iterator();

        while(venueIterator.hasNext())
        {
        event.setVenueID(venueIterator.next().getVenueID());
        events = DAOFactory.getInstance().getEventDAO().getEventsDOByKeyword(event);
        }*/
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                new AdvanceSearchView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cmbEvent;
    private javax.swing.JComboBox cmbLocation;
    private javax.swing.JComboBox cmbVenue;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblEvent;
    private javax.swing.JLabel lblFrom;
    private javax.swing.JLabel lblLocation;
    private javax.swing.JLabel lblTo;
    private javax.swing.JLabel lblVenue;
    private javax.swing.JTextField tfKeyword;
    // End of variables declaration//GEN-END:variables
    private DatePicker startDate;
    private DatePicker endDate;

    private void initDatePicker()
    {
        EmptyBorder border = new EmptyBorder(new Insets(10, 20, 10, 20));

        startDate = new DatePicker();

        JFormattedTextField startTextField = (JFormattedTextField) startDate.getComponent(0);
        startTextField.setFont(SearchView.getCustomFont());

        JButton startDateButton = (JButton) startDate.getComponent(1);
        startDateButton.setBorder(border);

        add(startDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 430, 200, 60));

        endDate = new DatePicker();

        JFormattedTextField endTextField = (JFormattedTextField) endDate.getComponent(0);
        endTextField.setFont(SearchView.getCustomFont());

        endDate.setFont(SearchView.getCustomFont());
        JButton endDateButton = (JButton) endDate.getComponent(1);
        endDateButton.setBorder(border);

        add(endDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 430, 200, 60));
    }
}
